
package net.sahv.bdyz.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.sahv.bdyz.mapper.FabricMapper;
import net.sahv.bdyz.mapper.MemberAttentionMapper;
import net.sahv.bdyz.mapper.PatternMapper;
import net.sahv.bdyz.model.Fabric;
import net.sahv.bdyz.model.MemberAttention;
import net.sahv.bdyz.model.Pattern;

@Service
public class MemberAttentionService {

	@Autowired
	private MemberAttentionMapper aemberAttentionMapper;// 关注
	@Autowired
	private FabricMapper fabricMapper;// 面料
	@Autowired
	private PatternMapper patternMapper;// 花型

	public int insert(MemberAttention record) {
		return aemberAttentionMapper.insert(record);
	}

	public List<MemberAttention> selectAll() {
		return aemberAttentionMapper.selectAll();
	}

	public int delete(Integer id) {
		return aemberAttentionMapper.deleteByPrimaryKey(id);
	}

	public MemberAttention findOne(Integer aId) {
		return aemberAttentionMapper.selectByPrimaryKey(aId);
	}

	public MemberAttention findBycollect(Integer pId, Integer mId) {
		return aemberAttentionMapper.findBycollect(pId,mId);
	}

	/**
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月12日 下午2:21:34
	 * @version 1.0
	 * @see 判断是否已关注
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	public boolean attentionOrNot(Integer mId, Integer type, Integer aId) {
		int count = aemberAttentionMapper.attentionOrNot(mId, type, aId);
		if (count > 0) {
			return true;
		}
		return false;
	}

	/**
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月12日 下午2:27:25
	 * @version 1.0
	 * @see 取消关注
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@Transactional
	public int cancelAttention(Integer mId, Integer type, Integer aId) {
		int count = aemberAttentionMapper.cancelAttention(mId, type, aId);
		if (type == 1) {
			Fabric f = fabricMapper.selectByPrimaryKey(aId);
			f.setfFocusOn(f.getfFocusOn() - 1);
			fabricMapper.updateByPrimaryKey(f);
		} else {
			Pattern p = patternMapper.selectByPrimaryKey(aId);
			p.setGuanzhudu(p.getGuanzhudu() - 1);
			patternMapper.updateByPrimaryKey(p);
		}
		return count;
	}

	/**
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月12日 下午2:30:53
	 * @version 1.0
	 * @see 添加关注
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@Transactional
	public int addAttention(Integer mId, Integer type, Integer aId) {
		MemberAttention mAtt = new MemberAttention();
		mAtt.setAttid(aId);
		mAtt.setAtttype(type);
		mAtt.setCreatetime(new Date());
		mAtt.setIsdelete(0);
		mAtt.setMid(mId);
		int count = aemberAttentionMapper.insert(mAtt);
		if (type == 1) {
			Fabric f = fabricMapper.selectByPrimaryKey(aId);
			f.setfFocusOn(f.getfFocusOn() + 1);
			fabricMapper.updateByPrimaryKey(f);
		} else {
			Pattern p = patternMapper.selectByPrimaryKey(aId);
			p.setGuanzhudu(p.getGuanzhudu() + 1);
			patternMapper.updateByPrimaryKey(p);
		}
		return count;
	}

}
